package com.x.erp.manage;

import java.util.Date;
import java.util.List;

import com.x.erp.dao.NoticeDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.x.framework.manage.BaseManage;

import com.x.erp.model.Notice;
import com.x.erp.model.NoticeMarket;
import com.x.erp.dao.MarketDao;
import com.x.erp.model.Market;

@Service
public class NoticeManage extends BaseManage {

    @Autowired
    private MarketDao marketDao;

    @Autowired
    private NoticeDao noticeDao;

    public Notice insertNotice(Notice notice,String marketId) throws Exception {
        notice.setNoticeCreateTime(this.getDate(0));
        notice.setNoticeUpdateTime(this.getDate(0));
        notice = this.insert(notice);
        String noticeId = notice.getNoticeId();
        NoticeMarket noticeMarket = new NoticeMarket();
        if (StringUtils.isNotEmpty(marketId)) {
            noticeMarket.setNoticeId(notice.getNoticeId());
            noticeMarket.setMarketId(marketId);
            this.insert(noticeMarket);
        } else {
            Market market = new Market();
            market.setMarketId("1");
            List<Market> marketList = marketDao.selectMarketList(market);
            noticeDao.insertNoticeMarketList(notice, marketList);
        }
        return notice;
    }

    public void updateNotice(Notice notice,String marketId) throws Exception {
        notice.setNoticeUpdateTime(new Date());
        this.update(notice);

        String noticeId = notice.getNoticeId();
        NoticeMarket noticeMarket = new NoticeMarket();
        noticeMarket.setNoticeId(noticeId);
        this.delete(noticeMarket);
        if (StringUtils.isNotEmpty(marketId)) {
            noticeMarket.setNoticeId(notice.getNoticeId());
            noticeMarket.setMarketId(marketId);
            this.insert(noticeMarket);
        } else {
            Market market = new Market();
            market.setMarketId("1");
            List<Market> marketList = marketDao.selectMarketList(market);
            noticeDao.insertNoticeMarketList(notice, marketList);
        }
    }

}
